/*
 *this file is part of nseer erp
 *Copyright (C)2006-2010 Nseer(Beijing) Technology co.LTD/http://www.nseer.com 
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either
 *version 2 of the License, or (at your option) any later version.
 */
package qcs.complain;

import include.nseer_cookie.counter;
import include.nseer_cookie.getFileLength;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.SmartUpload;

public class check_ok extends HttpServlet {

    public synchronized void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession dbSession = request.getSession();
        JspFactory _jspxFactory = JspFactory.getDefaultFactory();
        PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
        ServletContext dbApplication = dbSession.getServletContext();
        try {
            HttpSession session = request.getSession();
            ServletContext context = session.getServletContext();
            String path = context.getRealPath("/");
            counter count = new counter(dbApplication);
            SmartUpload mySmartUpload = new SmartUpload();
            mySmartUpload.setCharset("UTF-8");
            nseer_db_backup1 qcs_db = new nseer_db_backup1(dbApplication);
            if (qcs_db.conn((String) dbSession.getAttribute("unit_db_name"))) {
                mySmartUpload.initialize(pageContext);
                String file_type = getFileLength.getFileType((String) session.getAttribute("unit_db_name"));
                long d = getFileLength.getFileLength((String) session.getAttribute("unit_db_name"));
                mySmartUpload.setMaxFileSize(d);
                mySmartUpload.setAllowedFilesList(file_type);
                String complain_id = request.getParameter("complain_id");
                try {
                    mySmartUpload.upload();
                    String suggestion = mySmartUpload.getRequest().getParameter("suggestion");
                    String[] file_name = new String[mySmartUpload.getFiles().getCount()];
                    for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
                        com.jspsmart.upload.SmartFile file = mySmartUpload.getFiles().getFile(i);
                        if (file.isMissing()) {
                            file_name[i] = "";
                            continue;
                        }
                        int filenum1 = count.read((String) dbSession.getAttribute("unit_db_name"), "qcsAttachmentcount");
                        count.write((String) dbSession.getAttribute("unit_db_name"), "qcsAttachmentcount", filenum1);
                        file_name[i] = filenum1 + file.getFileName();
                        file.saveAs(path + "qcs/file_attachments/" + filenum1 + file.getFileName());
                    }
                    String sql = "update qcs_complain set suggestion='" + suggestion + "',attachment2='" + file_name[0] + "',dealwith_tag='1' where complain_id='" + complain_id + "'";
                    qcs_db.executeUpdate(sql);
                    qcs_db.commit();
                    qcs_db.close();
                    response.sendRedirect("qcs/complain/check_ok.jsp?finished_tag=0");
                } catch (Exception ex) {
                    response.sendRedirect("qcs/complain/check_ok_a.jsp?complain_id=" + complain_id);
                }
            } else {
                response.sendRedirect("error_conn.htm");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
} 